import { NextRequest } from 'next/server';
import { ResponseUtil } from '@/utils/response';
import { verifyAuth } from '@/utils/auth';
import { alipayService } from '@/services/alipay';

export async function POST(req: NextRequest) {
  try {
    const { user } = await verifyAuth(req);
    if (!user) {
      return ResponseUtil.unauthorized('请先登录');
    }

    const body = await req.json();
    const { orderNo, totalAmount, subject, body: tradeBody } = body;

    // 验证必要参数
    if (!orderNo || !totalAmount || !subject) {
      return ResponseUtil.error('缺少必要参数');
    }

    // 直接创建支付宝支付表单
    const formHtml = await alipayService.createPaymentForm({
      outTradeNo: orderNo,
      totalAmount,
      subject,
      body: tradeBody || `订单号: ${orderNo}`,
    });

    return ResponseUtil.success({
      formHtml
    });
  } catch (error) {
    console.error('创建支付宝支付表单失败:', error);
    return ResponseUtil.serverError('创建支付宝支付表单失败');
  }
} 